package com.example2016.cjj.scanqrcode;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;

public class MainActivity extends AppCompatActivity {
    private TextView coderesult_tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = (Button) findViewById(R.id.scan_btn);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //初始化
                IntentIntegrator integrator = new IntentIntegrator(MainActivity.this);
                integrator.setOrientationLocked(false);
                integrator.initiateScan();
            }
        });

        coderesult_tv = (TextView) findViewById(R.id.coderesult_tv);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        IntentResult result = IntentIntegrator.parseActivityResult(requestCode,resultCode,data);
        if(result!=null){
            if(result.getContents() ==null){
                    Toast.makeText(this, "取消了", Toast.LENGTH_LONG).show();
                } else {
                    coderesult_tv.setText("码值: " + result.getContents());
                    //Toast.makeText(this, "码值: " + result.getContents(), Toast.LENGTH_LONG).show();
                }

        }else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }
}
